<!--
 * @Descripttion: 
 * @version: 
 * @Author: 尹鹏孝
 * @Date: 2021-12-10 15:48:17
 * @LastEditors: 尹鹏孝
 * @LastEditTime: 2021-12-10 15:58:40
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>二叉树的最小深度</title>
</head>

<body>
    <script>
        // 最大深度和最小深度唯一不同是最小深度是左右孩子都是空时候才说明到了最低点，如果一个孩子不为空则不是最低点
        class Node {
            constructor(val, left, right) {
                this.val = val || null;
                this.left = left || null;
                this.right = right || null;
            }
            minDept(root) {
                if (root === null) {
                    return 0;
                }
                let queue = [root];
                let deepTh = 0;
                while (queue.length) {
                    let n = queue.length;
                    deepTh++;
                    for (let i = 0; i < n; i++) {
                        let node = queue.shift();
                        // 如果左右节点都是null则节点深度是最小
                        if (node.left == null && node.right === null) {
                            return deepTh;
                        }
                        node.left && queue.push(node.left);
                        node.right && queue.push(node.right);
                    }
                }
                return deepTh;
            }
        }

    </script>

</body>

</html>